package com.ghp.common.utils.response;

import java.io.Serializable;

import static com.ghp.common.utils.response.ResponseStatus.SERVICE_ERROR;

/**
 * @author ghp
 * @title 统一全局响应
 * @description
 */
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 封装数据
     */
    private T data;

    // 构造方法

    public ResponseResult() {
        this.code = ResponseStatus.SUCCESS.getCode();
        this.message = ResponseStatus.SUCCESS.getMessage();
    }

    public ResponseResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // getter和setter

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    // 正确响应

    public static ResponseResult success() {
        ResponseResult result = new ResponseResult();
        return result;
    }

    public static ResponseResult success(int code, String message) {
        return new ResponseResult(code, message);
    }

    public static ResponseResult success(Object data) {
        ResponseResult result = new ResponseResult();
        result.setData(data);
        return result;
    }

    public static ResponseResult success(ResponseStatus codeEnum) {
        return new ResponseResult(codeEnum.getCode(), codeEnum.getMessage());
    }


    // 错误响应

    public static ResponseResult fail(String message) {
        return new ResponseResult(SERVICE_ERROR.getCode(), message);
    }

    public static ResponseResult fail(int code, String message) {
        return new ResponseResult(code, message);
    }

    public static ResponseResult fail(ResponseStatus codeEnum) {
        return new ResponseResult(codeEnum.getCode(), codeEnum.getMessage());
    }
}
